package com.jzy.message.activities.base;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.blankj.utilcode.utils.EncryptUtils;
import com.google.gson.Gson;
import com.jzy.message.activities.bean.ReturnStatusResultEntity;
import com.jzy.message.service.JsonCallback;
import com.jzy.message.util.common.CommUtility;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.request.BaseRequest;

import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/6/13.
 */
public class AppConfig {
    public static Boolean bIsLogin = false;    //是否已经登录
    public static String sUserName = "";       //用户名
    public static String sPassword = "";       //密码
    public static String sUserID = "";         //用户ID
    public static String sUserRealName = "";   //用户真实姓名
    public static String JASON_SERVICE_URL_TOUXIANG = "http://39.106.5.148/QlwManager/";
    public static String JASON_SERVICE_URL="http://39.106.5.148/QlwManager/appapi/";
    //外网配置
    //博客服务器地址远程(Linux)
    public static String JsonUrl="http://47.89.178.187:7080/yoowi/a3ms";
    //远程图片视频服务器IP
    public static String sPic_Video_ServerIP="http://47.89.178.187";
    //直播服务器外网
    public static String LiveIP="47.89.178.187";//直播IP
    public static String LivePort="8080";//直播端口
    public static String ServerIP="47.89.178.187";//用来识别在直播服务器url中用哪个
    //XMPP 远程地址
    public static final String XMPP_HOST = "47.93.98.67";
    public static final int XMPP_PORT = 5222;
    public static final String ServerName="@iz2zebqllya0p9mbm38nswz";



    public static final String GroupServerName="@conference.pc-20150313bquc";
    //视频会议服务器
    public static final String telepresence_server = "47.89.178.187";
    public static final String conference_port="8080";

    public static String sCookie="";
    public static String sCookie_Live="";
    public static String IMAGE_CACHE_PATH = "imageloader/Cache"; // 图片缓存路径

    //cookie临时变量
    public static String sCurrentCookie="";
    public static String cookieName="_instafilk_un_";
    // 定义db文件的绝对路径
    public static String getDatabaseDirectory() {
//		return android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
        return "/data/data/com.openvideo/databases/";
    }
    //3d拍照参数
    public static String PHOTO3DFOLDER="photo";
    public static String getDatabaseFile() {
        return getDatabaseDirectory() + "/yoowi.db";

    }
    private static AppConfig inst = new AppConfig();

    public static AppConfig getInst() {
        return inst;
    }
    public interface Callback<T> {
        void onSuccess(T o);

        void onFailure(ReturnStatusResultEntity error);
    }
    public <T> void getHttpRequestJsonClass(String url, String token, final Class<T> Class, final Callback callback) {
        if (TextUtils.isEmpty(token)) {
            ReturnStatusResultEntity returnStatusResultEntity = new ReturnStatusResultEntity();
            returnStatusResultEntity.msg = "token不能为空";
            returnStatusResultEntity.isError = true;
            callback.onFailure(returnStatusResultEntity);
        }
        String timestamp = String.valueOf(System.currentTimeMillis());
        String content = token + timestamp + url;
        String sign = EncryptUtils.encryptMD5ToString(content).toLowerCase();
        OkHttpUtils.get(url)     // 请求方式和请求url
                .headers("token", token)
                .headers("timestamp", timestamp)
                .headers("sign", sign)
                .execute(new PangXieCallback<>(Class, callback));
    }
    /**
     * http的post提交方式
     *
     * @param url
     * @param token
     * @param data
     * @param Class
     * @param callback
     * @param <T>
     */
    public <T> void postHttpRequestJsonClass(String url, String token, Object data, final Class<T> Class, final Callback callback) {
        if (TextUtils.isEmpty(token)) {
            ReturnStatusResultEntity returnStatusResultEntity = new ReturnStatusResultEntity();
            returnStatusResultEntity.msg = "token不能为空";
            returnStatusResultEntity.isError = true;
            callback.onFailure(returnStatusResultEntity);
        }

        String timestamp = String.valueOf(System.currentTimeMillis());
        String jsonStr = new Gson().toJson(data);
      /*  try {
            jsonStr = URLEncoder.encode(jsonStr, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*/
        String content = token + timestamp + url + jsonStr;
        String sign = EncryptUtils.encryptMD5ToString(content).toLowerCase();
        OkHttpUtils.post(url)     // 请求方式和请求url
                .headers("token", token)
                .headers("timestamp", timestamp)
                .headers("sign", sign)
                .upJson(jsonStr)
                .execute(new PangXieCallback<>(Class, callback));
    }
    private class PangXieCallback<T> extends JsonCallback<T> {
        private Callback<T> callback;

        @Override
        public void onBefore(BaseRequest request) {
            super.onBefore(request);
        }

        @Override
        public void onAfter(@Nullable T t, @Nullable Exception e) {
            super.onAfter(t, e);
        }

        public PangXieCallback(Class<T> clazz, Callback<T> callback) {
            super(clazz);
            this.callback = callback;
        }

        public PangXieCallback(Type type, Callback<T> callback) {
            super(type);
            this.callback = callback;
        }

        @Override
        public void onSuccess(T t, Call call, Response response) {
            handleResponse(t, call, response, callback);
        }

        @Override
        public void onError(Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            handleError(call, response, e, callback);
        }
    }
    private <T> void handleResponse(T s, Call call, Response response, Callback<T> callback) {
        ReturnStatusResultEntity returnStatusResultEntity = CommUtility.getHttpStatusResult(response.code());
        if (!returnStatusResultEntity.isError) {
            callback.onSuccess(s);
        }  else {
            callback.onFailure(returnStatusResultEntity);
        }
    }
    private <T> void handleError(Call call, @Nullable Response response, @Nullable Exception e, Callback<T> callback) {
        ReturnStatusResultEntity returnStatusResultEntity = new ReturnStatusResultEntity();
        if (response == null) {
            returnStatusResultEntity.isError = true;
            returnStatusResultEntity.msg = "请检查网络";
            callback.onFailure(returnStatusResultEntity);
        } else {

            ReturnStatusResultEntity httpStatusResult = CommUtility.getHttpStatusResult(response.code());
            if (!httpStatusResult.isError) {
            }
        }
    }

}
